/*
 This file 'user' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2015 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2015-01-24 00:18
 */

define(function (require, exports, module) {

	require("../common/mediaqueries");

	var list = require("/ddd/list");

	var user = {};

	user.init = function () {

	};
	var App = require("Firebird.App");
	var Controller = require("Firebird.Controller");
	var Conditions = require("Firebird.data.Conditions");
	//var Component = require("Firebird.ui.Component");
	//var Grid = require("Firebird.ui.Grid");
	//var FormHelper = require("Firebird.ui.FormHelper");
	//var FormValidator = require("Firebird.ui.FormValidator");
	//var MultiSelecter = require("Firebird.ui.MultiSelecter");
	//var TreeSelecter = require("Firebird.ui.TreeSelecter");

	// ??? Controller
	var controller = new Controller({
		controller: "/fb-classic/permission",
		mapping: {
			save: "/user-save.html",
			edit: "/user-edit.html"
		}
	});

	/**
	 * history.Main
	 * @class history.Main
	 */
	Firebird.define("history.Main", {
		extend: "Firebird.App",
		controller: controller,
		builder: ".ui-builder",
		setContext: function () {
			var formConditions = this.context.formConditions = new Conditions({
				formId: "#J-searchForm"
			});

			var grid = this.context.grid = Firebird.create("Firebird.ui.Grid", {
				//caption: "Multiselect Example",
				id: "#J_viewGrid",
				conditions: formConditions,
				otherHeight: 330,//$(".ui-main-search").height() , //- $(window).height() ,
				controller: new Controller({
					controller: "/resource/fb-classic/js/layout",
					mapping: {
						search: "/navs.json"
					}
				}),
				//scroll: true,
				colModel: [
					{name: "id", hidden: true, width: 20, frozen: true},
					{name: "name", label: "名称", width: 100, align: "center", editable: true},
					{
						name: "id", label: "text", width: 100, align: "center", editable: true, edittype: "checkbox",
						editoptions: {
							value: "5:1"
						}
					},
					{name: "url", label: "地址", width: 200, align: "center"},
					{name: "pid", label: "pid", align: "center"}
				],

				gridComplete: function () {
					var ids = Firebird.Array.pluck(grid.getRawRow(), "id");
					$.each(ids, function (i, id) {
						grid.fire('editRow', id, true);
					});
				},

				multiboxonly: true,
				multiselect: true,
				//search: {
				//	caption: "搜索...",
				//	Find: "查找",
				//	Reset: "重置",
				//	odata: [
				//		{oper: "eq", text: "等于"},
				//		{oper: "ne", text: "不等"},
				//		{oper: "lt", text: "小于"},
				//		{oper: "le", text: "小于等于"},
				//		{oper: "gt", text: "大于"},
				//		{oper: "ge", text: "大于等于"},
				//		{oper: "bw", text: "开始于"},
				//		{oper: "bn", text: "不开始于"},
				//		{oper: "in", text: "属于"},
				//		{oper: "ni", text: "不属于"},
				//		{oper: "ew", text: "结束于"},
				//		{oper: "en", text: "不结束于"},
				//		{oper: "cn", text: "包含"},
				//		{oper: "nc", text: "不包含"}
				//	],
				//	groupOps: [
				//		{op: "AND", text: "所有"},
				//		{op: "OR", text: "任一"}
				//	]
				//},

				navGrid: {
					oper: {
						add: "", //定义是否启用添加操作的按钮；
						addicon: "", //用于添加操作的按钮上的图标，目前只能使用jQuery UI中的图标样式；
						addtext: "", //用于添加操作的按钮上的文本；
						addtitle: "", //用于添加操作的按钮上的tooltip；
						del: "", delicon: "", deltext: "", deltitle: "", //用于删除按钮的一套属性，可以参照添加按钮的几个属性；
						edit: "", editicon: "", edittext: "", edittitle: "", //用于编辑按钮的一套属性，可以参照添加按钮的几个属性；
						refresh: "", refreshicon: "", refreshtext: "", refreshtitle: "", //用于刷新表格按钮的一套属性，可以参照添加按钮的几个属性；当点击这个按钮时，将执行trigger("reloadGrid")并清除用于查询的参数；
						refreshstate: "", //定义如何重新载入Grid表格，firstpage：重新载入表格的第一页；current：重新载入当前页，并保存当前选中的行；默认为firstpage；
						afterRefresh: "", //定义点击刷新表格按钮之后的事件函数；
						beforeRefresh: "", //定义点击刷新表格按钮之前的事件函数；
						search: "", searchicon: "", searchtext: "", searchtitle: "", //用于查询按钮的一套属性，可以参照添加按钮的几个属性；当点击这个按钮时，将会调用searchGrid方法；
						view: "", viewicon: "", viewtext: "", viewtitle: "", //用于查看记录按钮的一套属性，可以参照添加按钮的几个属性；
						addfunc: "", //如果定义，则用定义的函数替代原有的add函数，这个函数将不接受任何参数；
						editfunc: "", //如果定义，则用定义的函数替代原有的edit函数，编辑的行的id作为参数传入这个函数；
						delfunc: "", //如果定义，则用定义的函数替代原有的del函数，编辑的行的id作为参数传入这个函数；
						alertcap: "", //当未选中任何行而点击编辑、删除、查看按钮时，弹出的提示框的标题；
						alerttext: "", //当未选中任何行而点击编辑、删除、查看按钮时，弹出的提示信息；
						cloneToTop: "" //定义是否“拷贝”一套导航按钮到Grid表格顶端的Pager中，默认为false；

					},
					edit: {},
					add: {},
					del: {},
					search: {},
					view: {}
				},

				columnChooser: {}

			});

			// Test
			// grid instanceof Firebird.ui.Grid // is true

			//debugger
			//grid.grid.jqGrid('navButtonAdd', '#J-column', {
			//	caption: "Columns",
			//	title: "Reorder Columns",
			//	onClickButton: function () {
			//		grid.grid.jqGrid('columnChooser');
			//	}
			//});

			context.vvv.coverFilter = function (nodes) {
				return "自定义显示：" + Firebird.Array.pluck(nodes, "id");
			}

		}
	});

	//require("_pluginPath_/bootstrap3/css/bootstrap.css");

	//require("_pluginPath_/jquery.ui/css/fb-theme-1/jquery-ui.css");

	//var tableRender = require("./table.tmpl");
	//var pager2 = require("_appPath_/desktop/fbos/plugin/pager/pager2");
	//
	//user.pager = new pager2.Pager2({
	//	renderTo: ".pager2",
	//	url: "/security/user/search",
	//	pageParam: {
	//		size: 6
	//	},
	//	lang: {
	//		first: "首页",
	//		prev: "上页",
	//		next: "下页",
	//		last: "尾页"
	//	},
	//	onSuccess: function (json) {
	//		$(".ui-main-grid-table").html(tableRender({
	//			userList: json.data
	//		}));
	//	}
	//});

	/**
	 * history.Save
	 * @class history.Save
	 */
	Firebird.define("history.Save", {
		extend: "Firebird.App",
		controller: controller,
		builder: ".ui-builder",
		setContext: function () {
			var me = this;
			var context = me.context;
			context.formValidator = new FormValidator({
				renderTo: "#J-form",
				onSuccess: function () {
					me.insert();
				}
			});

			//context.buttons = [
			//	{
			//		value: "确 定",
			//		autofocus: true,
			//		callback: function () {
			//			context.formValidator && context.formValidator.getResult();
			//			return false;
			//		}
			//	}    ,
			//	{
			//		value: "确 定",
			//		autofocus: true,
			//		callback: function () {
			//			context.formValidator && context.formValidator.getResult();
			//			return false;
			//		}
			//	},
			//	{
			//		value: "确 定",
			//		autofocus: true,
			//		callback: function () {
			//			context.formValidator && context.formValidator.getResult();
			//			return false;
			//		}
			//	}
			//];

			me.bindSaveForm();
		}
	});

	/**
	 * history.Edit
	 * @class history.Edit
	 */
	Firebird.define("history.Edit", {
		extend: "Firebird.App",
		controller: controller,
		builder: ".ui-builder",
		setContext: function () {
			var me = this;
			var context = me.context;
			context.formValidator = new FormValidator({
				id: "#J-form",
				onSuccess: function () {
					me.modify();
				}
			});
			me.bindEditForm();
		}
	});

	/**
	 * 告警
	 * @class history.WarnMain
	 */
	Firebird.define("history.WarnMain", {
		extend: "Firebird.App",
		controller: controller,
		builder: ".ui-builder",
		setContext: function () {
			var formConditions = this.context.formConditions = new Conditions({
				formId: "#J-searchForm"
			});

			controller.setMapping({
				search: "/warn/search"
			});

			var grid = this.context.grid = new Grid({
				id: "#J-viewGrid",
				conditions: formConditions,
				otherHeight: 240,
				controller: controller,
				colModel: [
					{name: "id", hidden: true, width: 1, frozen: true},
					{name: "name", label: "监测位置名称", align: "center"},
					{name: "chkDate", label: "告警时间", align: "center"},
					{
						name: "code", label: "告警内容", align: "center",
						formatter: function (val) {
							return val + "超限";
						}
					},
					{name: "val", label: "监测值", align: "center"},
					{name: "alertVal", label: "预警阀值", align: "center"}
				]
			});
		}
	});

	/**
	 * 告警统计
	 * @class history.WarnStatMain
	 */
	Firebird.define("history.WarnStatMain", {
		extend: "Firebird.App",
		controller: controller,
		builder: ".ui-builder",
		setContext: function () {

			$("#from").val(firebird.date.format({
				time: new Date,
				format: "$yyyy-$MM-$dd 00:00:00"
			}));

			$("#to").val(firebird.date.format({
				time: new Date,
				format: "$yyyy-$MM-$dd 23:59:59"
			}));

			var formConditions = this.context.formConditions = new Conditions({
				formId: "#J-searchForm"
			});

			controller.setMapping({
				search: "/warn/stat/search"
			});

			var grid = this.context.grid = new Grid({
				id: "#J-viewGrid",
				conditions: formConditions,
				otherHeight: 240,
				controller: controller,
				colModel: [
					{name: "id", hidden: true, width: 1, frozen: true},
					{name: "name", label: "监测点名称", align: "center"},
					{
						name: "chkDate", label: "监测时间段", align: "center", width: 298,
						formatter: function (val, grid, row) {
							return $("#from").val() + "到" + $("#to").val();
						}
					},
					{name: "code", label: "监测设备类型", align: "center", width: 90},
					{name: "maxinum", label: "最高值", align: "center", width: 60},
					{name: "count", label: "告警次数", align: "center"}
				]
			});
		}
	});

	/**
	 * 告警统计
	 * @class history.WarnStatMain
	 */
	Firebird.define("history.WarnIntervalMain", {
		extend: "Firebird.App",
		controller: controller,
		builder: ".ui-builder",
		setContext: function () {

			$("#from").val(firebird.date.format({
				time: new Date,
				format: "$yyyy-$MM-$dd 00:00:00"
			}));

			$("#to").val(firebird.date.format({
				time: new Date,
				format: "$yyyy-$MM-$dd 23:59:59"
			}));

			var formConditions = this.context.formConditions = new Conditions({
				formId: "#J-searchForm"
			});

			controller.setMapping({
				search: "/warn/stat/search"
			});

			var grid = this.context.grid = new Grid({
				id: "#J-viewGrid",
				conditions: formConditions,
				otherHeight: 240,
				controller: controller,
				colModel: [
					{name: "id", hidden: true, width: 1, frozen: true},
					{name: "name", label: "监测点名称", align: "center"},
					{
						name: "chkDate", label: "监测时间段", align: "center", width: 298,
						formatter: function (val, grid, row) {
							return $("#from").val() + "到" + $("#to").val();
						}
					},
					{name: "code", label: "监测设备类型", align: "center", width: 90},
					{name: "maxinum", label: "最高值", align: "center", width: 60},
					{
						name: "count", label: "超标时长（分钟）", align: "center", width: 110,
						formatter: function (val) {
							return ((val * 2) / 60).toFixed(2);
						}
					}
				]
			});
		}
	});

	//

	/**
	 * ????
	 */
	module.exports = {
		Main: history.Main,
		Save: history.Save,
		Edit: history.Edit,

		WarnMain: history.WarnMain,
		WarnStatMain: history.WarnStatMain,
		WarnIntervalMain: history.WarnIntervalMain
	}
});